package com.xxrl.shop.common.entity;

import java.io.Serializable;

/**
 * @author mis
 */
public class JsonResult<T extends Serializable> implements Serializable {
    private Boolean success;
    private Integer errorCode;
    private String errorMsg;
    private T data;

    public JsonResult() {
    }

    private void setUpSuccess() {
        errorCode = ResultCode.SUCCESS.getCode();
        errorMsg = ResultCode.SUCCESS.getMessage();
    }

    private void setUpFail(ResultCode resultEnum) {
        if (resultEnum == null) {
            errorCode = ResultCode.COMMON_FAIL.getCode();
            errorMsg = ResultCode.COMMON_FAIL.getMessage();
        } else {
            errorCode = resultEnum.getCode();
            errorMsg = resultEnum.getMessage();
        }
    }

    public JsonResult(boolean success) {
        this.success = success;
        if (success) {
            setUpSuccess();
        } else {
            setUpFail(null);
        }
    }

    public JsonResult(boolean success, ResultCode resultEnum) {
        this.success = success;
        if (success) {
            setUpSuccess();
        } else {
            setUpFail(resultEnum);
        }
    }

    public JsonResult(boolean success, T data) {
        this(success);
        this.data = data;
    }

    public JsonResult(boolean success, ResultCode resultEnum, T data) {
        this(success, resultEnum);
        this.data = data;
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public Integer getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(Integer errorCode) {
        this.errorCode = errorCode;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
